//
//  ICrEAMTool.m
//  ICrEAM
//
//  Created by mac on 16/10/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICrEAMTool.h"
#import "ICreamCache.h"
#import "Config.h"
#import "ParameterRespose.h"
#import "RepairTypeRespose.h"
#import "PrintAction.h"
#import "MyWebViewController.h"
#import "ICMTitleTextFeildView.h"


@implementation ICrEAMTool
#pragma mark - Convert

//+(BOOL)convertAMR:(NSString *)amrFilePath
//             toWAV:(NSString *)wavFilePath
//{
//    BOOL ret = NO;
//    BOOL isFileExists = [[NSFileManager defaultManager] fileExistsAtPath:amrFilePath];
//    if (isFileExists) {
//        [EMVoiceConverter amrToWav:amrFilePath wavSavePath:wavFilePath];
//        isFileExists = [[NSFileManager defaultManager] fileExistsAtPath:wavFilePath];
//        if (isFileExists) {
//            ret = YES;
//        }
//    }
//    
//    return ret;
//}
//
//+(BOOL)convertWAV:(NSString *)wavFilePath
//             toAMR:(NSString *)amrFilePath {
//    BOOL ret = NO;
//    BOOL isFileExists = [[NSFileManager defaultManager] fileExistsAtPath:wavFilePath];
//    if (isFileExists) {
//        [EMVoiceConverter wavToAmr:wavFilePath amrSavePath:amrFilePath];
//        isFileExists = [[NSFileManager defaultManager] fileExistsAtPath:amrFilePath];
//        if (!isFileExists) {
//            
//        } else {
//            ret = YES;
//        }
//    }
//    
//    return ret;
//}
+(void)savaOperationReceiptUserName:(NSString *)userName carSeries:(NSString*)series licence:(NSString *)licence status:(NSString *)status returnTime:(NSString *)returnTime{
    NSMutableArray *array=[NSMutableArray array];
    [array addObject:[NSString stringWithFormat:@"%@-%@",series,licence]];
    [array addObject:status];
    if (IsStrEmpty(returnTime)) {
        [array addObject:@""];
        [array addObject:@""];
    }else{
        //        NSDate *fromdate=[NSDate dateWithTimeIntervalSince1970:[returnTime longLongValue]/1000];
        NSDateFormatter *fomatter=[[NSDateFormatter alloc]init];
        NSTimeZone* timeZone = [NSTimeZone systemTimeZone];
        [fomatter setTimeZone:timeZone];
        fomatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";
        NSDate *fromdate=[fomatter dateFromString:returnTime];
        [array addObject:returnTime];
        NSTimeInterval ite=[[NSDate date] timeIntervalSinceDate:fromdate];
        if (ite<0) {
            int day=ite/(24*3600);
            if (day<-7) {
                [array addObject:[NSString stringWithFormat:@"%d天",day]];
            }else{
                NSTimeInterval reduceDay=ite-day*24*3600;
                int hour=reduceDay/3600;
                
                NSTimeInterval reduceMinute=reduceDay-hour*3600;
                int minute=reduceMinute/60;
                if (day==0) {
                    [array addObject:[NSString stringWithFormat:@"%d小时%d分钟",abs(hour),abs(minute)]];
                }else{
                    [array addObject:[NSString stringWithFormat:@"%d天%d小时%d分钟",abs(day),abs(hour),abs(minute)]];
                }
            }
        }else{
            int day=ite/(24*3600);
            if (day>7) {
                [array addObject:[NSString stringWithFormat:@"超时%d天",day]];
            }else{
                NSTimeInterval reduceDay=ite-day*24*3600;
                int hour=reduceDay/3600;
                NSTimeInterval reduceMinute=reduceDay-hour*3600;
                int minute=reduceMinute/60;
                if (day==0) {
                    [array addObject:[NSString stringWithFormat:@"超时%d小时%d分钟",abs(hour),abs(minute)]];
                }else{
                    [array addObject:[NSString stringWithFormat:@"超时%d天%d小时%d分钟",abs(day),abs(hour),abs(minute)]];
                }
            }
        }
        
    }
    //    [[ICreamCache sharedInstance] saveArray:array forKey:HomePagePath];
    //      [[ICreamCache sharedInstance] saveString:userName forKey:CustomName];
    [[NSNotificationCenter defaultCenter]postNotificationName:HomePageNotification object:nil userInfo:@{@"homepageArr":array,@"userName":userName}];
    
}
+(SeriesModel *)querySeriesModelWithSerieCode:(NSString *)seriesCode{
    NSArray *brands=[ICrEAMTool queryBrandSeries];
    for (BrandModel *brandModel in brands) {
        for (SeriesModel *seriesModel in brandModel.TM_SERIES) {
            if ([seriesModel.SERIES_CODE isEqualToString:seriesCode]) {
                return seriesModel;
            }
        }
    }
    return nil;
}
+(TMModel *)queryTMModelWithSerieCode:(NSString *)seriesCode ModelCode:(NSString *)modelCode {
    SeriesModel *seriesModel=[self querySeriesModelWithSerieCode:seriesCode];
    for (TMModel *tmModel in seriesModel.TM_MODEL) {
        if ([tmModel.MODEL_CODE isEqualToString:modelCode]) {
            return tmModel;
        }
    }
    return nil;
}
+ (NSString*)resultAboutAmoun:(CGFloat)f{
    ICrEAMUser *user = [ICrEAMUser shareICrEAMUser];
//    21381001	2138	四舍五入	结算圆整方式类别
//    21381002	2138	只舍不入	结算圆整方式类别
//    21381003	2138	只入不舍	结算圆整方式类别
    RoundWayType type;
    switch ([user.BALANCED_ACCURACY integerValue]) {
        case 21371001:{//    21371001	2137	元	结算精度类别
            switch ([user.BALANCED_WAY integerValue]) {
                case 21381001:{
                      type=YuanDefualtRoundWayType;
                }
                    break;
                case 21381002:{
                      type=YuanDownRoundWayType;
                }
                    break;
                case 21381003:{
                    type=YuanUpRoundWayType;
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 21371002:{//    21371002	2137	角	结算精度类别
            switch ([user.BALANCED_WAY integerValue]) {
                case 21381001:{
                    type=JiaoDefaultRoundWayType;
                }
                    break;
                case 21381002:{
                    type=JiaoDownRoundWayType;
                }
                    break;
                case 21381003:{
                    type=JiaoUpRoundWayType;
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 21371003:{//    21371003	2137	分	结算精度类别
            type=FenRoundWayType;
        }
            break;
            
        default:
            break;
    }
    return [self resultAboutAmountWith:f type:type];
}



//圆整方式
+ (NSString *)resultAboutAmountWith:(CGFloat)f type:(RoundWayType)type{
    NSString *str=[NSString stringWithFormat:@"%f",f];
    NSRange ran=[str rangeOfString:@"."];
    if (ran.length==1) {
        NSString *resultstr=[str substringToIndex:ran.location+3];
        NSString *resultYuan=[str substringToIndex:ran.location];
        NSInteger resultFenEnd=[[str substringWithRange:NSMakeRange(ran.location+2, 1)] integerValue];
        NSInteger resultJiaoEnd=[[str substringWithRange:NSMakeRange(ran.location+1, 1)] integerValue];
        switch (type) {
            case YuanUpRoundWayType:{
                NSInteger resultEnd=[[str substringWithRange:NSMakeRange(ran.location+1, 1)] integerValue];
                if (resultEnd>0) {
                    NSInteger yuanUp=[resultYuan integerValue]+1;
                    return [NSString stringWithFormat:@"%.2f",(CGFloat)yuanUp];
                }else{
                    return [NSString stringWithFormat:@"%@.00",resultYuan];
                }
            }break;
            case YuanDefualtRoundWayType:{
                NSInteger resultEnd=[[str substringWithRange:NSMakeRange(ran.location+1, 1)] integerValue];
                if (resultEnd>4) {
                    NSInteger yuanUp=[resultYuan integerValue]+1;
                    return [NSString stringWithFormat:@"%.2f",(CGFloat)yuanUp];
                }else{
                    return [NSString stringWithFormat:@"%@.00",resultYuan];
                }
                
            }break;
            case YuanDownRoundWayType:
                return [NSString stringWithFormat:@"%@.00",resultYuan];
                break;
            case JiaoUpRoundWayType:{
                if (resultFenEnd>0) {
                    resultJiaoEnd+=1;
                }
                return [NSString stringWithFormat:@"%@.%@",resultYuan,[NSString stringWithFormat:@"%ld0",resultJiaoEnd]];
            } break;
            case JiaoDefaultRoundWayType:{
                
                if (resultFenEnd>4) {
                    
                    resultJiaoEnd+=1;
                }
                return [NSString stringWithFormat:@"%@.%@",resultYuan,[NSString stringWithFormat:@"%ld0",(long)resultJiaoEnd]];
            }break;
            case JiaoDownRoundWayType:
                return [NSString stringWithFormat:@"%@.%@",resultYuan,[NSString stringWithFormat:@"%ld0",resultJiaoEnd]];
                break;
            case FenRoundWayType:
                return  resultstr;
                break;
                
            default:
                break;
        }
    }
    return nil;
}
+(BOOL)isVW{
    NSString *str=[[ICreamCache sharedInstance] stringForKey:isVWPath];
    if ([str isEqualToString:@"VW"]) {
        return YES;
    }else{
        return NO;
    }
}
+(BOOL)isZoreText:(NSString *)text{
    NSString *str=@"(^0+)|(^0+\\.0{0,2}$)";
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
    return [predicate evaluateWithObject:text];
    
}
+(BOOL)isPhoneNumber:(NSString *)phone{
    NSString *str=@"^1[3578]\\d{9}";
    //    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
    NSRegularExpression *reg=[NSRegularExpression  regularExpressionWithPattern:str options:0 error:nil];
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    [reg enumerateMatchesInString:phone options:0 range:NSMakeRange(0, phone.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
        [arr addObject:[phone substringWithRange:result.range]];
    }];
    
    return arr.count>0?1:0;
}
+(BOOL)ismileageText:(NSString *)mileageText{
    NSString *str=@"^(([1-9]+[0-9]*)|([0-9]+\\.[0-9]{1,2}))$";
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
    return [predicate evaluateWithObject:mileageText];
}

+(BOOL)isCarNumText:(NSString *)carText{
    //    NSString *str=@"^[A-Z]{1}[A-Z0-9]{5}";
    //    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
    //    return [predicate evaluateWithObject:carText];
    return YES;
}
+(BOOL)isVinText:(NSString *)vin{
    NSString *str=@"[A-Z0-9]{17}";
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
    return [predicate evaluateWithObject:vin];
}


+(NSArray*)getRepiarTypeArray{
    return [[ICreamCache sharedInstance] arrayForKey:RepairTypePath];
}
+(NSArray*)queryRepairOrderTypes{
    return [[ICreamCache sharedInstance] arrayForKey:RepairOrderTypePath];
}
//获取维修类型的编码
+ (NSString *)getRepiarTypeCodeWithRepiarName:(NSString *)repiarName{
    NSString *codeString;
    NSArray *allLargeRepiarTypes = [self getRepiarTypeArray];
    for (RepairTypeList *largeRepiarType in allLargeRepiarTypes) {
        NSArray *repiarTypeList = largeRepiarType.dicts;
        for (RepairTypeModel *repiarTypeModel in repiarTypeList) {
            if ([repiarTypeModel.REPAIR_TYPE_NAME isEqualToString:repiarName]) {
                codeString = repiarTypeModel.REPAIR_TYPE_CODE;
            }
        }
    }
    return codeString;
}
//获取维修类型的描述
+ (NSString *)getRepiarTypeDescWithRepiarCode:(NSString *)code{
    NSString *desc;
    NSArray *allLargeRepiarTypes = [self getRepiarTypeArray];
    for (RepairTypeList *largeRepiarType in allLargeRepiarTypes) {
        NSArray *repiarTypeList = largeRepiarType.dicts;
        for (RepairTypeModel *repiarTypeModel in repiarTypeList) {
            if ([repiarTypeModel.REPAIR_TYPE_CODE isEqualToString:code]) {
                desc = repiarTypeModel.REPAIR_TYPE_NAME;
            }
        }
    }
    return desc?desc:@"";
}

+(NSArray *)getLicencePrefixArray{
    return [[ICreamCache sharedInstance] arrayForKey:BrandPath];
}
+(NSArray*)getProvinceCityArray{
    return [[ICreamCache sharedInstance] arrayForKey:ProvinceCityPath];
}

+(NSArray*)getParameterArrayWithCode:(NSString *)code{
    NSArray *arr=[[ICreamCache sharedInstance] arrayForKey:DictionaryPath];
    for (Parameter *parameter in arr) {
        if ([parameter.statusType isEqualToString:code]) {
            return parameter.dicts;
        }
    }
    return nil;
}

+(NSArray*)getParameterTitleArrayWithArray:(NSArray *)array{
    if (array) {
        NSMutableArray *dataArray = [NSMutableArray array];
        for (ParameterModel *model in array) {
            if ([model.code isEqualToString:@"25271001"]) {
                continue;
            }
            [dataArray addObject:model.desc];
        }
        return dataArray;
    }
    return nil;
}
+(NSArray*)getParameterCodeArrayWithArray:(NSArray *)array{
    if (array) {
        NSMutableArray *dataArray = [NSMutableArray array];
        for (ParameterModel *model in array) {
            if ([model.code isEqualToString:@"25271001"]) {
                continue;
            }
            [dataArray addObject:model.code];
        }
        return dataArray;
    }
    return nil;
}




+(NSString*)getDeatilCodeWithParameterDesc:(NSString*)desc withCode:(NSString*)code{
    
    NSArray *arr=[self getParameterArrayWithCode:code];
    for (ParameterModel *model in arr) {
        if ([model.desc isEqualToString:desc]) {
            return model.code;
        }
    }
    return nil;
}
+(NSArray *)queryBrandSeries{
    return [[ICreamCache sharedInstance] arrayForKey:BrandSeriesPath];
}

+(NSArray *)queryIndustry
{
    return [[ICreamCache sharedInstance] arrayForKey:IndustryPath];
}

+(ParameterModel*)getParameterWithCode:(NSString*)detailCode{
    if (IsStrEmpty(detailCode)) {
        return nil;
    }
    NSString *code=[detailCode substringToIndex:4];
    NSArray *arr=[self getParameterArrayWithCode:code];
    for (ParameterModel *model in arr) {
        if ([model.code isEqualToString:detailCode]) {
            return model;
        }
    }
    return nil;
}

+ (ParameterModel *)getCodeModelForCode:(NSString *)code{
    if (code.length == 8) {
        NSString *subCode=[code substringToIndex:4];
        NSArray *arr=[self getParameterArrayWithCode:subCode];
        for (ParameterModel *model in arr) {
            if ([model.code isEqualToString:code]) {
                return model;
            }
        }
    }
    return nil;
}

+(UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}

//yyyy-mm-dd hh:mm 转成date
+ (NSDate *)getDateWithDateDesc:(NSString *)dateDesc{
//    dateDesc = @"09-12-2016";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];//yyyy-mm-dd hh:mm
    NSDate *date = [dateFormatter dateFromString:dateDesc];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    dateDesc = [self getDateHourAndMinWithTimeInterval:[@(date.timeIntervalSince1970*1000)stringValue]];
    return date;
}

//将世界时间转化为中国区时间
+ (NSDate *)worldTimeToChinaTime:(NSDate *)date
{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    NSDate *localeDate = [date dateByAddingTimeInterval:interval];
    return localeDate;
}

//时间戳字符串转小时和分钟
+ (NSString *)getDateHourAndMinuteWithTimeInterval:(NSString *)dateString{
    dateString = [@([dateString longLongValue]) stringValue];
    if (StrIsZero_gdm(dateString)) {
        return @"";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH-mm"];
    double time = [dateString doubleValue]/1000;
    NSDate *TimeDate = [NSDate dateWithTimeIntervalSince1970:time];
    return [formatter stringFromDate:TimeDate];
}

+ (NSString *)getDateHourMinuteWithTimeInterval:(NSString *)dateString{
    dateString = [@([dateString longLongValue]) stringValue];
    if (StrIsZero_gdm(dateString)) {
        return @"";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    double time = [dateString doubleValue]/1000;
    NSDate *TimeDate = [NSDate dateWithTimeIntervalSince1970:time];
    return [formatter stringFromDate:TimeDate];
}


+(NSString *)getDateWithTimeInterval:(NSString *)date{

//    NSString *str=[NSString stringWithFormat:@"%@",date];
    if ([date isKindOfClass:[NSNull class]]) {
        return @"";
    }
    date = [@([date longLongValue]) stringValue];
    if (StrIsZero_gdm(date)) {
        return @"";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    long long time = [date longLongValue]/1000;
    NSDate *TimeDate = [NSDate dateWithTimeIntervalSince1970:time];
    return [formatter stringFromDate:TimeDate];
}

+(NSString *)getDateHourAndMinWithTimeInterval:(NSString *)date{
    date = [@([date longLongValue]) stringValue];
    if (StrIsZero_gdm(date)) {
        return @"";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    long long time = [date longLongValue]/1000;
    NSDate *TimeDate = [NSDate dateWithTimeIntervalSince1970:time];
    return [formatter stringFromDate:TimeDate];
}

+(NSString *)getDateHourAndMinSecondWithTimeInterval:(NSString *)date{
    date = [@([date longLongValue]) stringValue];
    if (StrIsZero_gdm(date)) {
        return @"";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    long long time = [date longLongValue]/1000;
    NSDate *TimeDate = [NSDate dateWithTimeIntervalSince1970:time];
    return [formatter stringFromDate:TimeDate];
}

+(NSString *)getStringWithDate:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    return [formatter stringFromDate:date];
}

+(NSString *)getNowDate{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *TimeDate = [NSDate date];
    return [formatter stringFromDate:TimeDate];
}

+ (NSString *)getNowDateAndHour_Min{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *TimeDate = [NSDate date];
    return [formatter stringFromDate:TimeDate];
}

+(NSString *)dateToString:(NSDate *)date{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    return dateString;
    
}
+(NSDate *)tomorrowDate{
    NSDate *currentDate = [NSDate date];     // 由于时区的转换不需要获取本地系统的时间
    NSTimeInterval tomorrowDay = (24 * 60 * 60)*1;
    NSDate *tomorrowDate = [currentDate dateByAddingTimeInterval:tomorrowDay];
    
    return tomorrowDate;
    
}

+(NSDate *)stringToDate:(NSString *)dateString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:dateString];
    NSLog(@"%@", date);
    return date;
}
+ (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate
{
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    //转为现在时间
    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];
    return destinationDateNow;
}

//将UTC日期字符串转为本地时间字符串
//输入的UTC日期格式2013-08-03T04:53:51+0000
+(NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd 'T' HH:mm:ss Z"];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:localTimeZone];
    
    NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
    //输出格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
    
    return dateString;
}
+(NSDate *)getLocalFromUTC:(NSString *)utc
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *ldate = [dateFormatter dateFromString:utc];
    
    return ldate;
}


+ (void)showPopoverSelectionViewWithSourceView:(UIView *)sourceView displayWidth:(CGFloat)width displayHeight:(CGFloat)height dataArray:(NSMutableArray *)dataArray delegate:(id<PopoverSelectionViewControllerDelegate>)delegate showType:(PopoverSelectionViewShowType)showType {
    PopoverSelectionViewController *psvc = [[PopoverSelectionViewController alloc] init];
    psvc.modalPresentationStyle = UIModalPresentationPopover;
    psvc.popoverPresentationController.sourceView = sourceView;
    psvc.popoverPresentationController.sourceRect = CGRectMake(0, 0, sourceView.width, 0);
    if (showType == PopoverSelectionViewShowTypeTable) {
        psvc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown;
    } else if (showType == PopoverSelectionViewShowTypeCollection) {
        psvc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    }
    
    psvc.displayWidth = width;
    psvc.displayHeight = height;
    psvc.dataArray = dataArray;
    psvc.delegate = delegate;
    psvc.showType = showType;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"popoverSelectionView" object:psvc];
}

+ (void)showPopoverSelectionViewWithSourceView:(UIView *)sourceView displayWidth:(CGFloat)width displayHeight:(CGFloat)height dataArray:(NSMutableArray *)dataArray delegate:(id<PopoverSelectionViewControllerDelegate>)delegate showType:(PopoverSelectionViewShowType)showType IsMultiSelect:(BOOL)isMultiSelect WithSelectedStr:(NSString *)str WithPermittedArrowDirections:(NSString *)direct{
    PopoverSelectionViewController *psvc = [[PopoverSelectionViewController alloc] init];
    psvc.isMultiSelect = isMultiSelect;
    psvc.selectedStr = [[NSMutableString alloc] initWithString:str];
    psvc.modalPresentationStyle = UIModalPresentationPopover;
    psvc.popoverPresentationController.sourceView = sourceView;
    psvc.popoverPresentationController.sourceRect = CGRectMake(0, 0, sourceView.width, sourceView.height);
    if (showType == PopoverSelectionViewShowTypeTable) {
        psvc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown;
        if(direct && [direct isEqualToString:@"UP"])
        {
            psvc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
        }
    } else if (showType == PopoverSelectionViewShowTypeCollection) {
        psvc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    }
    psvc.displayWidth = width;
    psvc.displayHeight = height;
    psvc.dataArray = dataArray;
    psvc.delegate = delegate;
    psvc.showType = showType;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"popoverSelectionView" object:psvc];
}

//判断数组中是否有其他数组中的元素
+ (BOOL)array:(NSArray*)array hasItemInOtherArray:(NSArray*)otherArray{
    BOOL isIn = NO;
    for (NSString *item in array) {
        if ([otherArray containsObject:item]) {
            isIn = YES;
        }
    }
    return isIn;
}
#pragma mark 打印跳转
+(void)ipadPrintWithPrintType:(PrintType)type printId:(NSString*)printId Controller:(UIViewController *)ctr andEstimatePrice:(NSString *)price{
    MyWebViewController *web=[[MyWebViewController alloc]init];
    web.printType=type;
    NSString *title;
    NSString *urlStr;
    NSString *parmeter;
    switch (type) {
        case GetCarPrintType:
            title=@"接车单打印预览";
            urlStr=ReceptionPrintUrl;
            parmeter=@"RECEPTION_NO";
            break;
        case ProxyPrintType:
            title=@"委托书打印预览";
            urlStr=OrderPrintUrl;
            
            parmeter=@"RO_NO";
            break;
        case BalancePrintType:
            title=@"结算单打印预览";
            urlStr=BalancePrintUrl;
            parmeter=@"BALANCE_NO";
            break;
        default:
            break;
    }
    web.hideSuspendView=YES;
    web.hideRightNavItem=YES;
    web.title=title;
    NSString *baseUrl=[NSString stringWithFormat:@"http://%@",[[ICreamCache sharedInstance] stringForKey:kIPPath]];
    if (type==ProxyPrintType) {
         web.urlStr=[NSString stringWithFormat:@"%@%@?%@=%@&PRINT_PRE_AMOUNT=%@",baseUrl,urlStr,parmeter,printId,price];
    }else{
    web.urlStr=[NSString stringWithFormat:@"%@%@?%@=%@",baseUrl,urlStr,parmeter,printId];
    }
    
    [ctr.navigationController pushViewController:web animated:YES];
}

#pragma 增大button点击热区
+ (void)createAndAddBiggerButtonForButton:(UIButton*)button andSEL:(SEL)selector target:(id)target addTop:(float)top bottom:(float)bottom left:(float)left right:(float)right inView:(UIView *)superView{
    UIButton *biggerButton = [[UIButton alloc]initWithFrame:CGRectMake(button.leftEdge-left, button.topEdge-top, button.width+left+right, button.height+top+bottom)];
    [biggerButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:nil selectedBackImage:nil actionSEL:selector target:target];
    biggerButton.backgroundColor = [UIColor clearColor];
    [superView addSubview:biggerButton];
}

#pragma 校验身份证
+ (BOOL)judgeIdentityStringValid:(NSString *)identityString
{
    
    if (identityString.length != 18) return NO;
    // 正则表达式判断基本 身份证号是否满足格式
    NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
    NSPredicate *identityStringPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    //如果通过该验证，说明身份证格式正确，但准确性还需计算
    if(![identityStringPredicate evaluateWithObject:identityString]) return NO;
    
    //** 开始进行校验 *//
    
    //将前17位加权因子保存在数组里
    NSArray *idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
    
    //这是除以11后，可能产生的11位余数、验证码，也保存成数组
    NSArray *idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
    
    //用来保存前17位各自乖以加权因子后的总和
    NSInteger idCardWiSum = 0;
    for(int i = 0;i < 17;i++) {
        NSInteger subStrIndex = [[identityString substringWithRange:NSMakeRange(i, 1)] integerValue];
        NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
        idCardWiSum+= subStrIndex * idCardWiIndex;
    }
    
    //计算出校验码所在数组的位置
    NSInteger idCardMod=idCardWiSum%11;
    //得到最后一位身份证号码
    NSString *idCardLast= [identityString substringWithRange:NSMakeRange(17, 1)];
    //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
    if(idCardMod==2) {
        if(![idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"]) {
            return NO;
        }
    }
    else{
        //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
        if(![idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]]) {
            return NO;
        }
    }
    return YES;
}
#pragma mark    检测是否内容为空
+ (BOOL)isEmptyForContentText:(ICMTitleTextFeildView*)textFeildView{
    return !(textFeildView.contentText && ![textFeildView.contentText isEqualToString:@""]);
}
#pragma mark    校验个人姓名
+ (BOOL)isVaildRealName:(NSString *)realName
{
    if (IsStrEmpty(realName)) return NO;

    NSRange range1 = [realName rangeOfString:@"·"];
    NSRange range2 = [realName rangeOfString:@"•"];
    if(range1.location != NSNotFound ||   // 中文 ·
       range2.location != NSNotFound )    // 英文 •
    {
        //一般中间带 `•`的名字长度不会超过15位，如果有那就设高一点
        if ([realName length] < 2 || [realName length] > 15)
        {
            return NO;
        }
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[\u4e00-\u9fa5]+[·•][\u4e00-\u9fa5]+$" options:0 error:NULL];
        
        NSTextCheckingResult *match = [regex firstMatchInString:realName options:0 range:NSMakeRange(0, [realName length])];
        
        NSUInteger count = [match numberOfRanges];
        
        return count == 1;
    }
    else{
        //一般正常的名字长度不会少于2位并且不超过8位，如果有那就设高一点
        if ([realName length] < 2 || [realName length] > 8) {
            return NO;
        }
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[\u4e00-\u9fa5]+$" options:0 error:NULL];
        
        NSTextCheckingResult *match = [regex firstMatchInString:realName options:0 range:NSMakeRange(0, [realName length])];
        
        NSUInteger count = [match numberOfRanges];
        
        return count == 1;
    }
}
#pragma mark    校验公司名字
+ (BOOL)isVaildCompanyName:(NSString *)companyName{
    
    if (companyName.length>=3) {
        return YES;
    }

    return NO;

}

#pragma mark    校验手机
+ (BOOL)validateMobile:(NSString *)mobile{
    
    // 130-139  150-153,155-159  180-189  145,147  170,171,173,176,177,178
//    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0-9])|(14[57])|(17[013678]))\\d{8}$";
    NSString *phoneRegex = @"^(1)\\d{10}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

#pragma mark    组织机构代码
+ (BOOL)isValidEntpCode:(NSString *)code{
    
    NSString *regex = @"^([0-9A-Z]){8}-[0-9|X]$";
    
    NSPredicate *Entpcode = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    return [Entpcode evaluateWithObject:code];
    
}
+ (NSString *)trimWhitespace:(NSString *)val{

    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" "];
    NSString *trimmedString = [val stringByTrimmingCharactersInSet:set];
    return trimmedString;

}





@end
